<template>
  <div class="all">
    <div class="title">关注</div>
    <div class="content">
      <van-tabs v-model="active" color="#45b7cb" @change="change">
        <van-tab name="1" title="租房">
          <div
            v-for="item in collecthouselist"
            v-show="collecthouselist.length !== 0"
            :key="item.index"
            class="house1"
            @click="jumptodetail(item.roomId, item)"
          >
            <van-image
              width="121px"
              height="112px"
              lazy-load
              :src="item.image"
              fit="cover"
              radius="5px"
            />
            <div class="intrduce">
              <span class="title1">{{ item.subdistrict }}</span>
              <span class="title2"
                ><span>{{ item.rentalWay }}</span
                >|<span>{{ item.houseType }}</span
                >|<span>{{ item.roomAcre }}</span
                >|<span>{{ item.floor }}</span></span
              >
              <span v-show="item.distance" class="title2"
                ><van-icon name="location" />{{ item.distance }}</span
              >
              <div class="tips">
                <span>{{ item.rentalWay }}</span>
                <span>{{ item.paymentMethod }}</span>
                <span>{{ item.houseType }}</span>
              </div>
              <span class="title3">{{ item.price }}</span>
            </div>
          </div>
          <div v-show="collecthouselist.length === 0" class="nohouse">
            <img src="../../assets/nohouse.png" alt="" />
            <div>您还未关注任何房源</div>
          </div>
        </van-tab>
        <van-tab name="2" title="二手房">
          <div
            v-for="(item, index) in collecthouselist"
            v-show="collecthouselist.length != 0"
            :key="index"
            class="house2"
            @click="jumptobuydetail(item.houseId, item)"
          >
            <van-image
              width="121px"
              height="112px"
              lazy-load
              :src="item.houseImage"
              fit="cover"
              radius="5px"
            />
            <div class="intrduce">
              <div class="title1">{{ item.subdistrict }}</div>
              <div class="title2">
                <span>{{ item.houseType }}</span
                >|<span>{{ item.coveredArea }}</span
                >|<span>{{ item.floor }}</span>
              </div>
              <div class="tips">
                <span v-show="item.aspectType">{{ item.aspectType }}</span>
                <span v-show="item.decoration">{{ item.decoration }}</span>
                <span v-show="item.houseGenre">{{ item.houseGenre }}</span>
              </div>
              <div class="title3">{{ item.listedPrice }}</div>
            </div>
          </div>
          <div v-show="collecthouselist.length == 0" class="nohouse">
            <img src="@/assets/nohouse.png" alt="" />
            <div>您还未关注任何房源</div>
          </div>
        </van-tab>
        <van-tab name="3" title="新房">
          <div
            v-for="(item, index) in collecthouselist"
            :key="index"
            @click="jumptonewdetail(item.houseId, item)"
          >
            <div v-show="collecthouselist.length != 0" class="house3">
              <img :src="item.image" alt="" />
              <div class="intrduce">
                <span class="title1">{{ item.subdistrict }}</span>
                <span class="title2">
                  <span>年限:{{ item.propertyTerm }}</span
                  ><span>容积率:{{ item.plotRatio }}</span>
                  <span v-show="item.greeningRate"
                    >绿化率:{{ item.greeningRate }}</span
                  >
                </span>
                <span class="title3" v-if="item.sellingPrice != '0元/m²'">{{
                  item.sellingPrice
                }}</span>
                <span class="title3" v-if="item.sellingPrice == '0元/m²'"
                  >待定</span
                >
              </div>
            </div>
          </div>
          <div class="nohouse" v-show="collecthouselist.length == 0">
            <img src="../../assets/nohouse.png" alt="" />
            <div>您还未关注任何房源</div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
    <!-- <van-overlay :show="show" class="zhe">
      <img src="@/assets/loudou.gif" alt="" width="50px" />
    </van-overlay> -->
    <div class="slot"></div>
  </div>
</template>

<script>
import {
  getcollectHouse,
  getBuycollectHouse,
  getNewcollectHouse,
} from "../../apis/house.js";
import local from "@/utils/local.js";
import {
  getHouseList,
  setHouseList,
  getHouseListBuy,
  setHouseListBuy,
} from "@/utils/auth";
export default {
  data() {
    return {
      active: "1",
      currentPage: 1,
      perPage: 10,
      collecthouselist: [],
      loading: false,
      // show:true
    };
  },
  created() {
    if (local.get("followactive")) {
      this.active = local.get("followactive");
    }
    this.getlist();
  },
  mounted() {
    window.addEventListener("scroll", this.showScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.showScroll);
  },
  methods: {
    jumptonewdetail(e, item) {
      this.$router.push({ path: "/newhousedetail", query: { newHouseId: e } });
    },
    back() {
      this.$router.go(-1);
    },
    jumptodetail(e, item) {
      const list = getHouseList();
      for (let i = 0; i < list.length; i++) {
        if (list[i].roomId === item.roomId) {
          list.splice(i, 1);
        }
      }
      list.unshift(item);
      setHouseList(list);
      this.$router.push({ path: "/housedetail", query: { roomId: e } });
    },
    jumptobuydetail(e, item) {
      const list = getHouseListBuy();
      for (let i = 0; i < list.length; i++) {
        if (list[i].houseId === item.houseId) {
          list.splice(i, 1);
        }
      }
      list.unshift(item);
      setHouseListBuy(list);
      this.$router.push({ path: "/buyhousedetail", query: { houseId: e } });
    },
    async getlist() {
      if (this.active === "1") {
        await getcollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((res) => {
          console.log(res);
          if (res.code == 200 || res.code == 201) {
            // this.show=false
          }
          if (res.result.list.length === 10) {
            this.loading = true;
          }
          this.collecthouselist = res.result.list;
        });
      } else if (this.active === "2") {
        await getBuycollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((res) => {
          if (res.result.list.length === 10) {
            this.loading = true;
          }
          this.collecthouselist = res.result.list;
        });
      } else if (this.active === "3") {
        await getNewcollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((res) => {
          console.log("新房关注列表", res);
          if (res.result.list.length === 10) {
            this.loading = true;
          }
          this.collecthouselist = res.result.list;
        });
      }
    },
    async change() {
      local.set("followactive", this.active);
      this.loading = false;
      this.currentPage = 1;
      if (this.active === "1") {
        await getcollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((res) => {
          if (res.result.list.length === 10) {
            this.loading = true;
          }
          this.collecthouselist = res.result.list;
        });
      } else if (this.active === "2") {
        await getBuycollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((res) => {
          if (res.result.list.length === 10) {
            this.loading = true;
          }
          this.collecthouselist = res.result.list;
        });
      } else if (this.active === "3") {
        await getNewcollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((res) => {
          console.log("新房关注列表", res);
          if (res.result.list.length === 10) {
            this.loading = true;
          }
          this.collecthouselist = res.result.list;
        });
      }
    },
    showScroll() {
      const _this = this;
      const scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      const windowHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      const scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      if (
        scrollTop + windowHeight >= scrollHeight - 20 &&
        this.active === "1" &&
        this.loading
      ) {
        this.currentPage++;
        getcollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((response) => {
          if (response.result.list.length !== 10) {
            _this.loading = false;
          }
          _this.collecthouselist = [
            ..._this.collecthouselist,
            ...response.result.list,
          ];
        });
      } else if (
        scrollTop + windowHeight >= scrollHeight - 20 &&
        this.active === "2" &&
        this.loading
      ) {
        this.currentPage++;
        getBuycollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((response) => {
          if (response.result.list.length !== 10) {
            _this.loading = false;
          }
          _this.collecthouselist = [
            ..._this.collecthouselist,
            ...response.result.list,
          ];
        });
      } else if (
        scrollTop + windowHeight >= scrollHeight - 20 &&
        this.active === "3" &&
        this.loading
      ) {
        this.currentPage++;
        getNewcollectHouse({
          currentPage: this.currentPage,
          perPage: this.perPage,
          houseCate: this.active,
        }).then((response) => {
          if (response.result.list.length !== 10) {
            _this.loading = false;
          }
          _this.collecthouselist = [
            ..._this.collecthouselist,
            ...response.result.list,
          ];
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.slot{
  height: 30px;
}
.zhe {
  background-color: #fff;
  width: 100vw;
  height: 100vh;
  z-index: 100000;
  display: flex;
  justify-content: center;
  align-items: center;
}
.title {
  padding: 10px 10px;
  font-size: 17px;
  box-sizing: border-box;
  width: 100%;
  z-index: 10000;
  text-align: center;
  border-bottom: 1px solid #f1f1f1;
}
.content {
  .house1 {
    display: flex;
    margin: 10px 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f1f1f1;
    img {
      width: 120px;
      height: 110px;
    }
    .intrduce {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 10px;
      .title1 {
        font-size: 16px;
        font-weight: 550;
        color: #333333;
      }
      .title2 {
        font-size: 12px;
        color: #a0a0a0;
        span {
          margin: 0 2px;
        }
      }
      .tips {
        span {
          font-size: 10px;
          display: inline-block;
          margin-right: 5px;
          padding: 0px 2px;
          border: 1px solid #45b7cb;
          color: #45b7cb;
        }
      }
      .title3 {
        color: #d43b33;
        font-size: 16px;
        font-weight: bold;
        span {
          font-size: 12px;
          margin-left: 3px;
        }
      }
    }
  }
  .house2 {
    background-color: #fff;
    margin: 0 15px;
    display: flex;
    padding: 10px;
    border-bottom: 1px solid #f1f1f1;
    img {
      width: 120px;
      height: 110px;
    }
    .intrduce {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 10px;
      width: 190px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      .title1 {
        font-size: 16px;
        font-weight: 550;
        color: #333333;
      }
      .title2 {
        font-size: 12px;
        color: #a0a0a0;
        span {
          margin: 0 2px;
        }
      }
      .tips {
        span {
          font-size: 10px;
          display: inline-block;
          margin-right: 5px;
          padding: 0px 2px;
          border: 1px solid #45b7cb;
          color: #45b7cb;
        }
      }
      .title3 {
        color: #d43b33;
        font-size: 16px;
        font-weight: bold;
        span {
          color: #999;
          font-size: 12px;
          margin-left: 6px;
        }
      }
    }
    .up {
      margin: 0px !important;
      font-size: 12px;
      vertical-align: super;
      transform: scale(calc(10 / 12));
    }
  }
  .house3 {
    background-color: #fff;
    margin: 0 15px;
    display: flex;
    padding: 10px;
    border-bottom: 1px solid #f1f1f1;
    img {
      width: 120px;
      height: 110px;
    }
    .intrduce {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 10px;
      width: 190px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      .title1 {
        font-size: 16px;
        font-weight: 550;
        color: #333333;
      }
      .title2 {
        font-size: 12px;
        color: #a0a0a0;
        span {
          margin: 0 2px;
        }
      }
      .tips1 {
        span {
          font-size: 10px;
          display: inline-block;
          margin-right: 5px;
          padding: 1px 2px;
          border: 1px solid #45b7cb;
          color: #45b7cb;
        }
      }
      .title3 {
        color: #d43b33;
        font-size: 18px;
        font-weight: bold;
        span {
          color: #999;
          font-size: 12px;
          margin-left: 3px;
        }
      }
    }
  }
  .nohouse {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      margin-top: 20px;
      width: 100px;
    }
    div {
      font-size: 16px;
      color: #333;
      font-weight: bold;
      margin-top: 20px;
    }
  }
}
</style>
